    package com.googlecode.napp.server;
    /**
     *  Needed for this is the Java DB
     *  CREATE  TABLE IF NOT EXISTS `rnd`.`ticket` (
      `ticketid` INT NOT NULL AUTO_INCREMENT ,
      `username` INT NULL ,
      `loc` VARCHAR(45) NULL ,
      `probleem` TEXT NULL ,
      `image` INT NULL ,
      `oncall` INT NULL ,
      `date_start` DATETIME NULL ,
      `date_end` DATETIME NULL ,
      `status` INT NULL ,
       PRIMARY KEY (`ticketid`) )
     ENGINE = InnoDB;
     */
    import java.sql.Connection;
import java.sql.Date;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
import java.sql.ResultSetMetaData;
     
     /**useless edit **/
    public class Db
    {
        private static String dbURL = "jdbc:mysql://localhost:3306/rnd";
        private static String tableName = "ticket";
        // jdbc Connection
        static Connection conn = null;
        static Statement stmt = null;
     
        public static void InitialiseDb()
        {
            createConnection();
            selectTable();
            shutdown();
        }
       
        private static void createConnection()
        {
            try
            {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                //Get a connection
                conn = DriverManager.getConnection(dbURL, "root", "151188");
            }
            catch (Exception except)
            {
                except.printStackTrace();
            }
        }
       
        public static boolean Query(String Query)
        {
        	boolean temp = false;
            try
            {
                stmt = conn.createStatement();
                 temp = stmt.execute(Query);
                stmt.close();
            }
            catch (SQLException sqlExcept)
            {
                sqlExcept.printStackTrace();
            }
            return temp;
        }
       
        public static String selectTable()
        {
        	String output = "";
            try
            {
            	
                stmt = conn.createStatement();
                ResultSet results = stmt.executeQuery("select * from " + tableName);
                ResultSetMetaData rsmd = results.getMetaData();
                int numberCols = rsmd.getColumnCount();
                for (int i=1; i<=numberCols; i++)
                {
                    //print Column Names
                    output += (rsmd.getColumnLabel(i)+"\t");  
                }
     
                output += ("\n-------------------------------------------------");
     
                while(results.next())
                {
                    int id = results.getInt(1);
                    int username = results.getInt(2);
                    String location = results.getString(3);
                    String Problem = results.getString(4);
                    int image = results.getInt(5);
                    int oncall = results.getInt(6);
                    String date_start = results.getString(7);
                    //String date_end = results.getString(8);
                    int status = results.getInt(8);
                    output += ("\n" + id + 
                    		"\t" + username + 
                    		"\t" + location +
                    		"\t" + Problem +
                    		"\t" + image +
                    		"\t" + oncall +
                    		"\t" + date_start +
                    		//"\t" + date_end +
                    		"\t" + status);
                }
                results.close();
                stmt.close();
            }
            catch (SQLException sqlExcept)
            {
                sqlExcept.printStackTrace();
            }
            
            return output;
        }
       
        private static void shutdown()
        {
            try
            {
                if (stmt != null)
                {
                    stmt.close();
                }
                if (conn != null)
                {
                    DriverManager.getConnection(dbURL + ";shutdown=true");
                    conn.close();
                }          
            }
            catch (SQLException sqlExcept)
            {
               
            }
     
        }
    }
